package cn.com.haopy.yue.ai.db.service.hos.impl;

import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskIllness;
import cn.com.haopy.yue.ai.db.mapper.arisk.AIRiskIllnessMapper;
import cn.com.haopy.yue.ai.db.service.BaseServiceImpl;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskIllnessService;
import cn.com.haopy.yue.ai.db.utils.SqlInjectionExtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2025/3/28 8:56
 */
@Service
public class AIRiskIllnessServiceImpl extends BaseServiceImpl<AIRiskIllnessMapper, AIRiskIllness> implements AIRiskIllnessService {
    @Override
    public Page<AIRiskIllness> querySelective(String name, String question, Integer page, Integer limit, String sort, String order) {

        if (SqlInjectionExtUtils.check(sort) || SqlInjectionExtUtils.check(order)) {
            throw new RuntimeException("存在非法字段");
        }
        QueryWrapper<AIRiskIllness> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(name)) {
            queryWrapper.lambda().like(AIRiskIllness::getName, name);
        }
        if (StringUtils.hasText(question)) {
            queryWrapper.lambda().like(AIRiskIllness::getQuestion, question);
        }
        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            queryWrapper.orderBy(true, "asc".equals(order), sort);
        }
        return super.page(new Page<>(page, limit), queryWrapper);
    }

    @Override
    public List<AIRiskIllness> listActiveAll() {
        QueryWrapper<AIRiskIllness> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AIRiskIllness::getIsActive, Boolean.TRUE);
        return super.list(queryWrapper);
    }

    @Override
    public List<AIRiskIllness> listActiveSex(Short sex) {
        QueryWrapper<AIRiskIllness> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AIRiskIllness::getIsActive, Boolean.TRUE);
        queryWrapper.lambda().and(wrapper -> {
            wrapper.eq(AIRiskIllness::getSex, 0).or().eq(AIRiskIllness::getSex, sex);
        });
        return super.list(queryWrapper);
    }
}
